package springtest.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.bson.Document;
import org.bson.types.BSONTimestamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;
import com.mongodb.CursorType;
import com.mongodb.DB;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCursor;

import springtest.pojo.Oplog;

/**
 * @author chenzhiqiang
 * @date 2018-03-05 14:14:44
 */
@Service
public class OplogService {
	@Autowired
	private MongoTemplate mongoTemplate;
	@Autowired
	@Qualifier("oplogMongoTemplate")
	private MongoTemplate oplogMongoTemplate;
	@Autowired
	private SolrTemplate solrTemplate;
	private static String solrDioPackageName = "springtest.pojo.";
	private static String solrDioEndName = "Dio";

	public void slaveMongo(Oplog oplog) {
		mongoTemplate.save(oplog, "OPLOG1");
	}

	public List<Oplog> getData() {
		org.springframework.data.solr.core.query.Criteria criteria = org.springframework.data.solr.core.query.Criteria
				.where("oplog_name");
		SimpleQuery query = new SimpleQuery(criteria);
		Iterator<Oplog> iterator = solrTemplate.query("new_core", query, Oplog.class).iterator();
		List<Oplog> list = new ArrayList<>();
		while (iterator.hasNext()) {
			Oplog oplog = iterator.next();
			list.add(oplog);
		}
		return list;
	}

	public Oplog saveData() {
		MongoClient mongoClient = new MongoClient("localhost", 27111);
		MongoTemplate template = new MongoTemplate(mongoClient, "tets");
		DB db = mongoClient.getDB("local");
		BSONTimestamp ts = null;
		DBCursor cursor = db.getCollection("oplog.rs").find();
		MongoCursor<Document> iterator = template.getCollection("oplog.rs").find().cursorType(CursorType.TailableAwait)
				.iterator();
		while (true) {
			while (cursor.hasNext()) {
				DBObject dbObject = cursor.next();
				// 得到的是数据库加集合名 中间.分开。
				String ns = (String) dbObject.get("ns");
				if (ns.trim() != null && ns.equals("tets.OPLOG1")) {
					String value = dbObject.get("o").toString();
					JSONObject jsonObject = JSON.parseObject(value);
					JSONObject jsonObject2 = jsonObject.getJSONObject("_id");
					// 得到了变化的数据的id
					String mongoId = jsonObject2.get("$oid").toString();
					// 记录上次查询的位置
					ts = (BSONTimestamp) dbObject.get("ts");
					Criteria criteria = Criteria.where("_id").is(mongoId);
					Query query = new Query(criteria);
					List<Oplog> resultList = template.find(query, Oplog.class, "OPLOG1");
					Oplog findById = template.findById(mongoId, Oplog.class);
					if (resultList.size() != 0) {
						Oplog oplog = resultList.get(0);
						solrTemplate.saveBean("new_core", oplog);
						solrTemplate.commit("new_core");
					}
				}
			}
			cursor = db.getCollection("oplog.rs").find(new BasicDBObject("ts", new BasicDBObject("$gt", ts)));
		}
	}

	public void testTailableCursor() {
		// MongoClient mongoClient = new MongoClient("localhost", 27111);
		// MongoTemplate template = new MongoTemplate(mongoClient, "tets");
		// DB db = mongoClient.getDB("local");
		// BSONTimestamp ts = null;
		// DBCursor cursor = db.getCollection("oplog.rs").find();
		MongoCursor<Document> cursor = oplogMongoTemplate.getCollection("oplog.rs").find()
				.cursorType(CursorType.TailableAwait).iterator();
		while (cursor.hasNext()) {
			Document dbObject = cursor.next();
			// 得到的是数据库加集合名 中间.分开。
			String ns = (String) dbObject.get("ns");
			if (ns.trim() != null && ns.equals("tets.OPLOG1")) {
				Document object = (Document) dbObject.get("o");
				// 得到了变化的数据的id
				String mongoId = object.get("_id").toString();
				// 记录上次查询的位置
				// ts = (BSONTimestamp) dbObject.get("ts");
				Criteria criteria = Criteria.where("_id").is(mongoId);
				Query query = new Query(criteria);
				List<Oplog> resultList = mongoTemplate.find(query, Oplog.class, "OPLOG1");
				Update update = new Update();
				if (resultList.size() != 0) {
					Oplog oplog = resultList.get(0);
					solrTemplate.saveBean("new_core", oplog);
					solrTemplate.commit("new_core");
				}
			}
		}
		// cursor = db.getCollection("oplog.rs").find(new
		// BasicDBObject("ts", new BasicDBObject("$gt", ts)));

	}
}
